package com.yupi.springbootinit.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Description
 * @Author minify
 * @Date 2023/12/18 23:57
 * @Project mybi-backend
 */
public class JucCompletableFuture {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("111");
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return 1;
        }, executorService).handle((s, e) -> {
            int i = 1 / 0;
            System.out.println("222");
            return s + 2;
        }).handle((s, e) -> {
            System.out.println("333");
            return s + 3;
        }).whenComplete((s, e) -> {
            if (e == null) {
                System.out.println("计算结果：" + s);
            }
        }).exceptionally((e) -> {
            e.printStackTrace();
            return null;
        });

        System.out.println("主线程完成其他事情去了。。。");
        executorService.shutdown();
    }
}
